package com.mcintyret.site.journal;

import com.google.gson.annotations.Expose;
import com.mcintyret.site.users.OwnedByUser;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import java.util.LinkedList;
import java.util.List;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */

@Entity
@Table(name = "numerical_sequences")
public class Timeseries implements OwnedByUser {

    private Integer id;
    @Expose
    private String name;
    private String username;
    private Journal journal;
    private String description;
    private int order;
    @Expose
    private List<Datapoint> datapoints = new LinkedList<Datapoint>();

    // For use by Hibernate
    Timeseries() { }

    public Timeseries(String name) {
        this(name, null);
    }

    public Timeseries(String name, String description) {
        setName(name);
        setDescription(description);
    }

    @Id
    @GeneratedValue
    @Column(name = "numerical_sequence_id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name = "journal_id")
    public Journal getJournal() {
        return journal;
    }

    public void setJournal(Journal journal) {
        this.journal = journal;
    }

    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "order_num")
    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public void setDatapoints(List<Datapoint> datapoints) {
        this.datapoints = datapoints;
    }

    public Timeseries addDataPoint(Datapoint datapoint) {
        datapoints.add(datapoint);
        datapoint.setTimeseries(this);
        datapoint.setUsername(username);
        return this;
    }

    @OneToMany(orphanRemoval = true, mappedBy = "timeseries")
    @OrderBy("time")
    @Cascade(CascadeType.ALL)
    public List<Datapoint> getDatapoints() {
        return datapoints;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Timeseries that = (Timeseries) o;

        if (order != that.order) {
            return false;
        }
        if (description != null ? !description.equals(that.description) : that.description != null) {
            return false;
        }
        if (journal != null ? !journal.equals(that.journal) : that.journal != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = journal != null ? journal.hashCode() : 0;
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + order;
        return result;
    }
}
